#include <gui/GuiBase.h>

primary::GuiBase::GuiBase()
{
	this->context = ImGui::CreateContext();
	this->bind();
}

primary::GuiBase::~GuiBase()
{
	ImGui::DestroyContext(this->context);
}

void primary::GuiBase::bind()
{
	assert(this->context != nullptr);
	ImGui::SetCurrentContext(this->context);
}

void primary::GuiBase::render()
{
	this->new_frame();
	for (auto& item : this->windows)
	{
		item->render();
	}
	ImGui::Render();
	this->draw();
}

void primary::GuiBase::push_window(GuiWindowBase* win)
{
	this->windows.push_back(win);
}

void primary::GuiBase::pop_window(GuiWindowBase* win)
{
	utl::vector_delete_element(this->windows, win);
}

primary::GuiWindowBase::GuiWindowBase(const std::string& tit) : title{ tit }
{}

void primary::GuiWindowBase::render()
{
	ImGui::PushID(this);
	if (ImGui::Begin(this->title.c_str()))
	{
		this->content();
		ImGui::End();
	}
	ImGui::PopID();
}

bool* primary::GuiWindowBase::muti_is_show()
{
	return &this->is_show;
}
